var jCategoryTree;


//点击树节点action
function clickTreeItemAction(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	if(treeItem){
		editCategoryForm(treeItem.id);
	}
	
}


/*******************************************
1.ajax请求的页面中，在发起AJAX请求前，初始化编辑器[KE.init({})]，否则无法使用编辑器
2.ajax返回HTML后，在回调函数里执行KE.create()创建编辑器
3.详见/StoreAdmin/web/scripts/kindeditor/kindeditor-init.js
*******************************************/
function loadHtmlEditor(){
	createHtmlEditor('categoryDescription');	
}
//根据树节点显示按钮
function displayButtons(categoryId){
	if(categoryId == null){
		//如果没有什么节点
		disableIconBtn("moveUp");
		disableIconBtn("moveDown");
		disableIconBtn("add");
		disableIconBtn("delete");
		disableIconBtn("save");
	}else if(categoryId == ""){
		//如果是新增节点则只开发保存按钮
		disableIconBtn("moveUp");
		disableIconBtn("moveDown");
		disableIconBtn("add");
		disableIconBtn("delete");
		enableIconBtn("save");
	}else if(categoryId == "1"){
		//如果是根节点则屏蔽删除按钮
		disableIconBtn("moveUp");
		disableIconBtn("moveDown");
		enableIconBtn ("add");
		disableIconBtn("delete");
		enableIconBtn ("save");
	}else{
		//开放全部
		enableIconBtn ("moveUp");
		enableIconBtn ("moveDown");
		enableIconBtn ("add");
		enableIconBtn ("delete");
		enableIconBtn ("save");
	}

}
//加载目录编辑form
function editCategoryForm(categoryId,callback){
	displayButtons();//先将所有按钮disable	
	showCategoryForm(__ctxPath+"/catalog/giftCategory/blank.html?doAction=edit&categoryId="+categoryId,loadHtmlEditor);
}
//新增目录
function createCategory(){
	displayButtons();//先将所有按钮disable
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	if (treeItem) {
		if (parseInt(treeItem.depth) < 10) {
			$j("#categoryFormDiv").empty();
			//在AJAX调用前初始化编辑器;并在回调函数使用KE.create()方法创建编辑器
			initHtmlEditor('categoryDescription', 600, 300);
			showCategoryForm(__ctxPath + "/catalog/giftCategory/blank.html?doAction=add&parentCategoryId=" + treeItem.id, loadHtmlEditor);
		}
		else {
			jAlert(__FMT.categoryList_overMaxDepth);
		}
	}
}

//在本页以ajaxPage方式打开categoryForm 
function showCategoryForm(url,callback){
	//在AJAX调用前初始化编辑器;并在回调函数使用KE.create()方法创建编辑器
	initHtmlEditor('categoryDescription',600,300);	
	$j("#categoryFormDiv").loading().load(url,null,callback);
}

//将目录置前
function upCategory(){
	var jNode = jCategoryTree.getSelected();
	if(jNode.length > 0 && jNode.prevAll("[id]").size()!=0){
		moveActiveCategoryUp(jCategoryTree);
		updateCategoriesSortOrder(jNode);
	}else{
		//已经是第一个元素，不需要再置前
	}
	
}
//将目录放后
function downCategory(){
	var jNode = jCategoryTree.getSelected();
	if(jNode.length > 0  && jNode.nextAll("[id]").size()!=0){
		moveActiveCategoryDown(jCategoryTree);
		updateCategoriesSortOrder(jNode);
	}else{
		//已经是最后一个元素，不需要再放后
	}
}

//更新目录排序
function updateCategoriesSortOrder(jNode){
	var categoires=jNode.parent("ul").children("li[id]");
	var paramData="";
	for(var i=0;i<categoires.length;i++){
		var id=categoires.get(i).id;
		paramData+="categoryIds="+id+"&";
	}
	$j.post(__ctxPath+"/catalog/giftCategory.html?doAction=updateCategoriesSortOrder",paramData);
}

//删除目录
function delCategory(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	if(treeItem&&confirm(__FMT.common_message_confirmDeleteThis+treeItem.name+"?")){
		var paramData={categoryId:treeItem.id};
		$j.post(__ctxPath+"/catalog/giftCategory.html?doAction=delete",paramData,delCategoryCallback,"json");
	}
}
//删除目录callback
function delCategoryCallback(data){
	sysMsg(data.message,(data.result!=1));
	if(data.result==1){
		//fill div
		deleteTreeItem(jCategoryTree);
		displayButtons();
		//start:显示父目录
		activeTreeItem(jCategoryTree,data.parentId);
		editCategoryForm(""+data.parentId);
		//end:显示父目录
	}
}
